﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Ex2.StudentsInfoNOSerialization
{
    public class StudentInfo
    {
        public void AddStudent(Student newStudent, string fileName)
        {
            using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Append)))
            {
                writer.Write(newStudent.FirstName);
                writer.Write(newStudent.LastName);
                writer.Write(newStudent.FacultyNumber);
            }
        }

        public void PrintAllStudents(string fileName)
        {
            using (BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open)))
            {
                while (reader.PeekChar() != -1)
                {
                    string firstName = reader.ReadString();
                    string lastName = reader.ReadString();
                    Int32 fn = reader.ReadInt32();
                    Console.WriteLine("First name: {0}, Last name: {1}, FN:{2}",firstName,lastName,fn);
                }
            }
        }

        public void DeleteStudent(int inputFN,string fileName)
        {
            string tempFileName = "temp";
            File.Create(tempFileName).Dispose();

            using (BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open)))
            {
                using (BinaryWriter writer = new BinaryWriter(new FileStream(tempFileName, FileMode.OpenOrCreate,
                    FileAccess.Write)))
                {
                    while (reader.PeekChar() != -1)
                    {
                        while (reader.PeekChar() != -1)
                        {
                            string firstName = reader.ReadString();
                            string lastName = reader.ReadString();
                            int fn = reader.ReadInt32();
                            if (fn != inputFN)
                            {
                                writer.Write(firstName);
                                writer.Write(lastName);
                                writer.Write(fn);
                            }
                        }
                    }
                }
            }

            File.Delete(fileName);
            File.Move(tempFileName, fileName);
        }
    }

    public class StudentsInfoTest
    {
        public static string FILE_NAME = "..\\..\\studentsInfo.bin";
        public static void CreateFile(string fileName)
        {
            File.Create(fileName).Dispose();
        }
        public static void Main()
        {
            CreateFile(FILE_NAME);
            StudentInfo info = new StudentInfo();
            Student gosho = new Student("Gosho", "Petrov", 61250);
            Student pesho = new Student("Pesho", "Obreikov", 61325);

            info.AddStudent(gosho, FILE_NAME);
            info.AddStudent(pesho, FILE_NAME);
            info.AddStudent(pesho, FILE_NAME);

            info.PrintAllStudents(FILE_NAME);
            Console.WriteLine();

            Console.WriteLine();
            info.DeleteStudent(61325, FILE_NAME);
            info.PrintAllStudents(FILE_NAME);

        }
    }
}
